//
//  NSString+Extension.m
//
//  Created by apple on 14-4-2.
//  Copyright (c) 2014年 gyh. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)
- (CGRect)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];
}

- (CGRect)sizeWithFont:(UIFont *)font lineSpace:(CGFloat)lineSpace maxSize:(CGSize)maxSize
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:lineSpace];//行间距

    NSDictionary *attrs = @{NSFontAttributeName : font,
                            NSParagraphStyleAttributeName:paragraphStyle};
    
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];

}

+(NSString *)countNumAndChangeformat:(NSString *)num
{
    int count = 0;
    long long int a = num.floatValue;
    while (a != 0)
    {
        count++;
        a /= 10;
    }
    
    NSArray *moneyList = [num componentsSeparatedByString:@"."];
    
    NSMutableString *string = [NSMutableString stringWithString:[moneyList firstObject]];
    NSMutableString *newstring = [NSMutableString string];
    while (count > 3) {
        count -= 3;
        NSRange rang = NSMakeRange(string.length - 3, 3);
        NSString *str = [string substringWithRange:rang];
        [newstring insertString:str atIndex:0];
        [newstring insertString:@"," atIndex:0];
        [string deleteCharactersInRange:rang];
    }
    [newstring insertString:string atIndex:0];
    
    return [NSString stringWithFormat:@"%@.%@", newstring, [moneyList lastObject]];
}

@end
